﻿@{
    ViewBag.Title = "Index";
}

<div style="width: 100%;">
    <div class="mini-toolbar" style="border-bottom: 0; padding: 0px;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 100%;">
                    <a class="mini-button" iconcls="icon-add" plain="true" onclick="addRow()">增加</a>
                    <a class="mini-button" iconcls="icon-remove" plain="true" onclick="removeRow()">删除</a>
                    <a class="mini-button" iconcls="icon-save" plain="true" onclick="saveData()">保存</a>
                    <a class="mini-button" iconcls="icon-edit" plain="true" onclick="editData()">修改</a>
                </td>
                <td style="white-space: nowrap;">
                    <input id="key" class="mini-textbox" emptytext="请输入关键字" style="width: 250px;" onenter="onKeyEnter" />
                    <a class="mini-button" plain="true" onclick="search()">查询</a>
                </td>
            </tr>
        </table>
    </div>
</div>

<div class="mini-fit" style="height: 100%;">

    <div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 100%;" url="Get"
        idfield="id" pagesize="20" allowcelledit="true" allowcellselect="true" multiselect="true"
        editnextonenterkey="true" editnextrowcell="true">
        <div property="columns">
            <div type="indexcolumn"></div>
            <div type="checkcolumn"></div>
            <div field="CustomerID" visible="false"></div>
            <div field="CustomerCode" width="80px">
                客户编号
            </div>
            <div field="CustomerName" width="100px">
                客户名称
            </div>

            <div field="MiniCode" width="100px">
                助记码
            </div>

            <div field="CPhoneNum" width="100px">
                电话号码
            </div>

            <div field="CHandPhone1" width="100px">
                手机号码1
            </div>

            <div field="CHandPhone2" width="100px">
                手机号码2
            </div>

            <div field="CWexin" width="100px">
                微信号
            </div>

            <div field="CQQ" width="100px">
                QQ
            </div>

            <div field="CFax" width="100px">
                传真
            </div>

            <div field="CEmail" width="100px">
                电子邮箱
            </div>

            <div field="CAddress" width="100px">
                客户地址
            </div>

            <div field="CLawyer" width="100px">
                法人
            </div>

            <div field="CFM" width="100px">
                财务经理
            </div>

            <div field="CBM" width="100px">
                业务经理
            </div>

            <div field="CType" width="100px" renderer="onCType">
                客户类型
            </div>

            <div field="CPayType" width="100px" renderer="onCPayType">
                付款类型
            </div>

            <div field="CLevel" width="100px" renderer="onCLevel">
                客户等级
            </div>
            <div field="CreditLimitM" width="100px">
                信用额度
            </div>
            <div field="CreditLimitD" width="100px">
                信用天数
            </div>
            <div field="IsServiceFee" type="checkboxcolumn" readonly="true" truevalue="1" falsevalue="0" width="100px">
                是否免服务费
            </div>
            <div field="CScore" width="100px">
                积分
            </div>
            <div field="CBankName" width="100px">
                银行名称
            </div>
            <div field="CBankCardNo" width="100px">
                银行卡号
            </div>
            <div field="Note" width="100px">
                备注
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    mini.parse();

    var grid = mini.get("datagrid1");

    grid.load();

    grid.frozenColumns(0, 2);

    var dtype, CPayType, CLevel;
    grid.on("preload", function () {
        if (!dtype)
            $.ajax({
                url: "/SystemData/Dict/Get?dtype=@HttpUtility.UrlEncode("客户类型")",
                async: false,
                success: function (f) {
                    dtype = mini.decode(f);
                }
            });
        if (!CPayType)
            $.ajax({
                url: "/SystemData/Dict/Get?dtype=@HttpUtility.UrlEncode("客户付款类型")",
                async: false,
                success: function (f) {
                    CPayType = mini.decode(f);
                }
            });
        if (!CLevel)
            $.ajax({
                url: "/SystemData/Dict/Get?dtype=@HttpUtility.UrlEncode("客户等级")",
                async: false,
                success: function (f) {
                    CLevel = mini.decode(f);
                }
            });
    });

    function search() {
        var key = mini.get("key").getValue();

        grid.load({ key: key });
    }

    function onKeyEnter(e) {
        search();
    }

    function addRow() {
        mini.open({
            url: "/Customer/Customer/IndexAdd",
            title: "添加客户",
            width: "980px",
            height: "390px",
            allowResize: false,
            allowDrag: true,
            ondestroy: function () { grid.reload(); }
        });
    }

    function removeRow() {
        var rows = grid.getSelecteds();
        if (rows.length > 0) {
            grid.removeRows(rows, true);
        }
    }

    function editData() {
        var row = grid.getSelected();
        if (!row) {
            top.mini.alert("请选中行");
            return;
        }
        mini.open({
            url: "/Customer/Customer/IndexEdit",
            title: "修改客户",
            width: "980px",
            height: "390px",
            allowResize: false,
            allowDrag: true,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { CustomerID: row.CustomerID };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function () { grid.reload(); }
        });
    }

    function saveData() {
        var removed = grid.getChanges("removed");
        if (removed.length <= 0) {
            top.mini.showTips({ content: "未做任何修改！！！", state: "info", x: "center", y: "center", timeout: 1000 });
            return;
        }

        var jsonDel = mini.encode(removed);
        grid.loading("保存中，请稍后......");
        $.ajax({
            url: "Del",
            data: { del: jsonDel },
            type: "post",
            success: function (text, flag, res) {
                grid.unmask();
                if (res.responseText == "") {
                    grid.reload();
                }
                else
                    top.mini.alert(res.responseText);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                mini.alert(errorThrown.responseText);
            }
        });
    }

    function onCType(e) {
        for (var i = 0, l = dtype.length; i < l; i++) {
            var g = dtype[i];
            if (g.DID == e.value) return g.DValue;
        }
        return "";
    }

    function onCPayType(e) {
        for (var i = 0, l = CPayType.length; i < l; i++) {
            var g = CPayType[i];
            if (g.DID == e.value) return g.DValue;
        }
        return "";
    }

    function onCLevel(e) {
        for (var i = 0, l = CLevel.length; i < l; i++) {
            var g = CLevel[i];
            if (g.DID == e.value) return g.DValue;
        }
        return "";
    }

</script>
